﻿#include "usernetworkaccessmanager.h"
#include <QDebug>
#include "user.h"

const static QString USER_BASE_URL = BASE_URL+"user/";
const static QString USER_LOGIN_URL = USER_BASE_URL+"login";
const static QString USER_SENDMESSAGE_URL = USER_BASE_URL+"sendMessage";
const static QString USER_REGISTER_URL = USER_BASE_URL+"register";
const static QString USER_UPDATE_USERINFO_URL = USER_BASE_URL+"updateUserInfo";
const static QString USER_CHANGE_PASSWORD_URL = USER_BASE_URL+"changePassword";
const static QString USER_UPDATE_USER_URL = USER_BASE_URL+"updateUser";
const static QString USER_FINDPASSWORD_URL = USER_BASE_URL+"findPassword";

UserNetworkAccessManager::UserNetworkAccessManager(QObject *parent) : BaseNetworkAccessManager(parent)
{

}

void UserNetworkAccessManager::login(QString username, QString password)
{
    QByteArray data;
    data.append("username="+username+"&");
    data.append("password="+password);

    doPost(USER_LOGIN_URL, data);
}

void UserNetworkAccessManager::sendMessage(QString mobile)
{
    QByteArray data;
    data.append("mobile="+mobile);

    doPost(USER_SENDMESSAGE_URL, data);
}

void UserNetworkAccessManager::doRegister(QString username, QString password, QString inviteCode){
    QByteArray data;
    data.append("username="+username+"&");
    data.append("password="+password+"&");
    data.append("inviteCode="+inviteCode);

    doPost(USER_REGISTER_URL, data);
}

void UserNetworkAccessManager::updateUserInfo()
{
    QString username = MySettings.value("user/username").toString();

    QByteArray data;
    data.append("username="+username);

    doPost(USER_UPDATE_USERINFO_URL, data);
}

void UserNetworkAccessManager::changePassword(
        QString oldPassword,
        QString newPassword,
        Tools::PasswordType passwordType)
{
    int type;
    if(passwordType == Tools::LoginPassword){
        type = 0;
    }else{
        type = 1;
    }

    QByteArray data;
    data.append("username="+User::getUsername()+"&");
    data.append("oldPassword="+oldPassword+"&");
    data.append("newPassword="+newPassword+"&");
    data.append(QString("passwordType=%1").arg(type));

    doPost(USER_CHANGE_PASSWORD_URL, data);
}

void UserNetworkAccessManager::updateUser(QString nickName, QString position, QString tel, QString mobile, QString QQ, QString wechat)
{
    QByteArray data;
    data.append("username="+User::getUsername()+"&");
    data.append("nickName="+nickName+"&");
    data.append("position="+position+"&");
    data.append("tel="+tel+"&");
    data.append("mobile="+mobile+"&");
    data.append("QQ="+QQ+"&");
    data.append("wechat="+wechat);

    doPost(USER_UPDATE_USER_URL, data);
}

void UserNetworkAccessManager::findPassword(QString username, QString password){
    QByteArray data;

    data.append("username="+username+"&");
    data.append("password="+password);

    doPost(USER_FINDPASSWORD_URL, data);
}
